package Hot100.Medium.HeapAndStack;

import java.util.Stack;

public class LC20_ValidBracket {

    public boolean isValid(String s) {
        if(s.length() % 2 != 0){
            return false;
        }
        Stack<Character> stack = new Stack();
        int len = s.length();
        for(int i = 0; i < len; i++){
            if(s.toCharArray()[i] == '(' ||s.toCharArray()[i] == '[' || s.toCharArray()[i] =='{'){
                stack.push(s.toCharArray()[i]);
            } else {
                if(stack.isEmpty()){
                    return false;
                }
                char top = stack.pop();
                if(s.toCharArray()[i] == ')' && top != '(' ||
                        (s.toCharArray()[i] == ']' && top != '[') ||
                        (s.toCharArray()[i] == '}' && top != '{')){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }


}
